<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once ( LIB_DIR.'search/abstractsearch.php');
/**
 * Description of shopsearch
 *
 * @author jszhang
 */
class ShopSearch  extends Search {

    public function ShopSearch( $host='127.0.0.1', $port=3312 ) {
        parent::__construct($host, $port);

        //override some member vars
        //$this->m_index = array( "shop_info_index", "shop_info_stemmed" );
        $this->m_index = array( "shop_info_index", "shop_info_stemmed" );
    }

    /**
     * override protected functions
     */
     
    //after some default value changed, invoke this function
    protected function initialize() {
		//clear all filters
		$this->m_sphinxClient->ResetFilters();
		$this->m_sphinxClient->ResetOverrides();
		$this->m_sphinxClient->ResetGroupBy();

        $this->m_sphinxClient->SetConnectTimeout ( 1 );
        $this->m_sphinxClient->SetMatchMode ( $this->m_matchMode );
        $this->m_sphinxClient->SetWeights ( array ( 100, 1 ) );

        //        if ( count($filtervals) )
        //            $cl->SetFilter ( $filter, $filtervals );
        //        if ( $groupby )
        //            $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
        //        if ( $sortby )
        //            $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );

        //        if ( $sortexpr )
        //            $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );

        //        if ( $distinct )
        //            $cl->SetGroupDistinct ( $distinct );
        //        if ( $select )
        //            $cl->SetSelect ( $select );

        $this->m_sphinxClient->SetRankingMode ( $this->m_ranker );
        $this->m_sphinxClient->SetArrayResult ( $this->m_isArrayResult );
        $this->m_perPage = SHOP_INFO_PER_PAGE;
    }

    //put your code here
    protected function printResult( $res ) {
        $result = array();
        
        $result['total'] = $res['total'];
        $result['total_found'] = $res['total_found'];
        $result['time'] = $res['time'];

//        print "Query stats:\n";
//        if ( is_array($res["words"]) )
//            foreach ( $res["words"] as $word => $info )
//                print "    '$word' found $info[hits] times in $info[docs] documents\n";
//        print "\n";

        if ( isset($res["matches"]) && is_array( $res["matches"] ) ) {
            $result['matchIds'] = array();
            foreach ( $res["matches"] as $docinfo ) {
//                print "$n. doc_id=$docinfo[id], weight=$docinfo[weight]";
//                foreach ( $res["attrs"] as $attrname => $attrtype ) {
//                    $value = $docinfo["attrs"][$attrname];
//                    if ( $attrtype & SPH_ATTR_MULTI ) {
//                        $value = "(" . join ( ",", $value ) .")";
//                    } else {
//                        if ( $attrtype==SPH_ATTR_TIMESTAMP )
//                            $value = date ( "Y-m-d H:i:s", $value );
//                    }
//                    print ", $attrname=$value";
//                }
//                print "\n";

                $result['matchIds'][] = $docinfo['id'];
            }
        }

        return $result;
    }

	protected function printGroupByResult( $res, $groupField ){
		$result = array();

        $result['total'] = $res['total'];
        $result['total_found'] = $res['total_found'];

		if ( isset($res["matches"]) && is_array( $res["matches"] ) ) {
            $result['matches'] = array();
            foreach ( $res["matches"] as $docinfo ) {
                $result['matches'][$docinfo['attrs']['@groupby']] = array(
					'count' => $docinfo['attrs']['@count']
				);
            }
        }
		return $result;
	}
	
	/**********************************************************
	 *	Seal Created by 2010-6-19
	 **********************************************************/
	public function GetHotShopWithSameCat( $catIdList, $platId, $count )
	{
		$this->Reset();
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		if( $platId < 100 )
			$this->m_sphinxClient->SetFilterRange( 'plat_id', 0, 99 );
		else
			$this->m_sphinxClient->SetFilterRange( 'plat_id', 100, 99999 );
			
		$this->m_sphinxClient->SetFilter( 'cate_id_list', explode(',', $catIdList) );
		
        $this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'review_count' );
        
        $re = $this->doSearchAttributeOnly();
        
       if( $re === false || !isset($re['matches']) )
        	return false;
        	
        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-6-19
	 **********************************************************/
	public function GetBestScoreShop( $count )
	{
		$this->Reset();
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilterRange( 'plat_id', 1, 4 );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'score' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Bo Created by 2010-7-24
	 **********************************************************/
	public function GetBestScoreMall( $count )
	{
		$this->Reset();
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilterRange( 'plat_id', 100, 9999);
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'score' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
		
	/**********************************************************
	 *	Seal Created by 2010-6-19
	 **********************************************************/
	public function GetBestRateShop( $count )
	{
		$this->Reset();
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilterRange( 'plat_id', 1, 4 );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'rate' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-6-19
	 **********************************************************/
	public function GetHotShopAndMall( $count, $type )
	{
		$this->Reset();
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		
		if( $type == 1 )
			$this->m_sphinxClient->SetFilterRange( 'plat_id', 1, 4 );
		else
			$this->m_sphinxClient->SetFilterRange( 'plat_id', 100, 9999 );
			
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'review_count' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-7-3
	 **********************************************************/
	public function GetHotShopByPlatId( $count, $platId )
	{
		$this->Reset();
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilter( 'plat_id', array($platId) );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'review_count' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-7-7
	 **********************************************************/
	public function GetShopCount( $platId )
	{
		$this->Reset();
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilter( 'plat_id', array($platId) );
		
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $re['total_found'];
	}
	
	/**********************************************************
	 *	Seal Created by 2010-7-7
	 **********************************************************/
	public function GetShopByUid( $uid, $count )
	{
		$this->Reset();
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilter( 'create_user_id', array($uid) );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'create_time' );
		
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-7-7
	 **********************************************************/
	public function GetTopShopListCount( $sortBy, $platId )
	{
		$this->Reset();
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		if( $platId != null )
			$this->m_sphinxClient->SetFilter( 'plat_id', array($platId) );
			
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, $sortBy );
		
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $re['total_found'];
	}
	
	/**********************************************************
	 *	Seal Created by 2010-7-7
	 **********************************************************/
	public function GetTopShopList( $sortBy, $platId, $offset, $count )
	{
		$this->Reset();
		//set result limit
        $this->m_sphinxClient->SetLimits ( $offset, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		if( $platId != null )
			$this->m_sphinxClient->SetFilter( 'plat_id', array($platId) );
			
		$this->m_sphinxClient->SetSortMode( SPH_SORT_ATTR_DESC, $sortBy );
		
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-8-8
	 **********************************************************/
	public function GetShopByKeyWord( $keyword, $isMall, $offset, $count )
	{
		$this->Reset();
		//set result limit
        $this->m_sphinxClient->SetLimits ( $offset, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		
		if( $isMall === false )
			$this->m_sphinxClient->SetFilterRange ( 'plat_id', 0, 99 );
		else 
			$this->m_sphinxClient->SetFilterRange( 'plat_id', 100, 999);
		
			
        $re = $this->doSearch2( $keyword );
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
}
?>
